﻿using System;
using System.Data.Entity;
using Quba.DataAccess.Repositories.Abstract;
using Quba.Entities.Database.General;
using Quba.Entities.Database.Question;
using Quba.Entities.Database.Tag;
using Quba.Entities.Database.User;
using Quba.Framework.Database;

namespace Quba.DataAccess.Repositories.Concrete
{
    public class QubaBaseRepositoty<T> : IQubaBaseRepository<T> where T : class
    {
        private readonly QubaContext _qubaContext;
        private BaseEntity _resultantEntity;

        public QubaBaseRepositoty()
        {
            _qubaContext = new QubaContext();
        }

        #region Add
        public BaseEntity Add(T entity)
        {
            if (typeof(T) == typeof(Admin))
            {
                _resultantEntity = _qubaContext.Admins.Add(entity as Admin);
                _qubaContext.SaveChanges();
                return _resultantEntity;

            }
            if (typeof(T) == typeof(Candidate))
            {
                _resultantEntity = _qubaContext.Candidates.Add(entity as Candidate);
                _qubaContext.SaveChanges();
                return _resultantEntity;
            }
            if (typeof(T) == typeof(Question))
            {
                _resultantEntity = _qubaContext.Questions.Add(entity as Question);
                _qubaContext.SaveChanges();
                return _resultantEntity;
            }

            if (typeof(T) == typeof(Option))
            {
                _resultantEntity = _qubaContext.Options.Add(entity as Option);
                _qubaContext.SaveChanges();
                return _resultantEntity;
            }
            if (typeof(T) == typeof(QuestionPaper))
            {
                _resultantEntity = _qubaContext.QuestionPapers.Add(entity as QuestionPaper);
                _qubaContext.SaveChanges();
                return _resultantEntity;
            }
            if (typeof(T) == typeof(QuestionType))
            {
                _resultantEntity = _qubaContext.QuestionTypes.Add(entity as QuestionType);
                _qubaContext.SaveChanges();
                return _resultantEntity;
            }

            if (typeof(T) == typeof(Tag))
            {
                _resultantEntity = _qubaContext.Tags.Add(entity as Tag);
                _qubaContext.SaveChanges();
                return _resultantEntity;
            }

            if (typeof(T) == typeof(AnswerPaper))
            {
                _resultantEntity = _qubaContext.AnswerPapers.Add(entity as AnswerPaper);
                _qubaContext.SaveChanges();
                return _resultantEntity;
            }
            return null;
        } 
        #endregion Add

        #region Delete
        public BaseEntity Delete(T entity)
        {
            if (typeof(T) == typeof(Admin))
            {
                _resultantEntity = _qubaContext.Admins.Remove(entity as Admin);
                _qubaContext.SaveChanges();
                return _resultantEntity;

            }
            if (typeof(T) == typeof(Candidate))
            {
                _resultantEntity = _qubaContext.Candidates.Remove(entity as Candidate);
                _qubaContext.SaveChanges();
                return _resultantEntity;
            }
            if (typeof(T) == typeof(Question))
            {
                _resultantEntity = _qubaContext.Questions.Remove(entity as Question);
                _qubaContext.SaveChanges();
                return _resultantEntity;
            }

            if (typeof(T) == typeof(Option))
            {
                _resultantEntity = _qubaContext.Options.Remove(entity as Option);
                _qubaContext.SaveChanges();
                return _resultantEntity;
            }
            if (typeof(T) == typeof(QuestionPaper))
            {
                _resultantEntity = _qubaContext.QuestionPapers.Remove(entity as QuestionPaper);
                _qubaContext.SaveChanges();
                return _resultantEntity;
            }
            if (typeof(T) == typeof(QuestionType))
            {
                _resultantEntity = _qubaContext.QuestionTypes.Remove(entity as QuestionType);
                _qubaContext.SaveChanges();
                return _resultantEntity;
            }

            if (typeof(T) == typeof(Tag))
            {
                _resultantEntity = _qubaContext.Tags.Remove(entity as Tag);
                _qubaContext.SaveChanges();
                return _resultantEntity;
            }

            if (typeof(T) == typeof(AnswerPaper))
            {
                _resultantEntity = _qubaContext.AnswerPapers.Remove(entity as AnswerPaper);
                _qubaContext.SaveChanges();
                return _resultantEntity;
            }
            return null;
        } 
        #endregion Delete

        #region Get
        public virtual T Get(int questionTypeId)
        {
            throw new NotImplementedException();
        }
        #endregion Get

        #region Update
        public virtual T Update(T admin)
        {
            throw new NotImplementedException();
        }
        #endregion Update

        #region GetQubaContext
        protected QubaContext _GetQubaContext()
        {
            return _qubaContext;
        }
        #endregion GetQubaContext

    }
}